#include <WiFi.h>
#include <TFT_eSPI.h> // Hardware-specific library
#include "lv_port_indev.h"
//#include "getdata.h"
#include "function.h"
#include "EEPROM.h"

LV_FONT_DECLARE(siyuanrouti_20);
//TFT_WIDTH*TFT_HEIGHT/4
static lv_disp_draw_buf_t draw_buf;    //定义显示器变量
static lv_color_t buf[TFT_WIDTH*TFT_HEIGHT/4]; //定义刷新缓存


TFT_eSPI tft = TFT_eSPI();       // Invoke custom library


/* Display flushing */
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
    uint32_t w = (area->x2 - area->x1 + 1);
    uint32_t h = (area->y2 - area->y1 + 1);

    tft.startWrite();                                        //使能写功能
    tft.setAddrWindow(area->x1, area->y1, w, h);             //设置填充区域
    tft.pushColors((uint16_t *)&color_p->full, w * h, true); //写入颜色缓存和缓存大小
    tft.endWrite();                                          //关闭写功能
    lv_disp_flush_ready(disp); //调用区域填充颜色函数
}


void Lvgl_TFT_Init(void){
    tft.init();         //初始化
    tft.setRotation(0); //屏幕旋转方向（横向）
    lv_init();
    lv_disp_draw_buf_init(&draw_buf, buf, NULL, TFT_WIDTH*TFT_HEIGHT/4);
    /*Initialize the display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init(&disp_drv);
    /*Change the following line to your display resolution*/
    disp_drv.hor_res = TFT_WIDTH;
    disp_drv.ver_res = TFT_HEIGHT;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register(&disp_drv);
    //initialize the input device
    lv_port_indev_init();
}


unsigned char i=0;
extern uint8_t yuyan_flag;    //默认英文
extern lv_obj_t * arc;//开机动画弧
extern lv_obj_t*label_open;//开机动画标签
lv_obj_t*open_back;//开机背景创建
lv_obj_t*label_open;//开机wifi连接提示标签
lv_timer_t * timer=NULL;//用于延时处理开机问题：网络连接和动画
lv_timer_t * timer1=NULL;//
lv_timer_t * timer2=NULL;//
lv_timer_t * timer4=NULL;//
extern uint8_t back_flag;
extern uint8_t lianan_flag;
extern lv_obj_t*label_open_anim;
extern unsigned char j;
//成功连接 初始化项目 删除动画
void open_task_suc(lv_timer_t * timer){
    lv_obj_del(arc);
    Project_Init();
}

//初始化失败  重启
void open_task_err(lv_timer_t * timer){
     ESP.restart();
}



//提示是否成功连接
void open_task_1(lv_timer_t * timer){
    lv_obj_del(open_back);
//成功连接wifi
    if(j<=20&&i<=10){
        Open_up_animation();
        timer1=lv_timer_create(open_task_suc, 8000, NULL);
        lv_timer_set_repeat_count(timer1,1);
    }
//未能成功连接wifi准备重新启动
    else{
        Open_up_animation();
        if(yuyan_flag){
        lv_label_set_text(label_open_anim,"failed,Reseting...");
        }
        else
        lv_label_set_text(label_open_anim,"连接失败,重启中...");
        timer2=lv_timer_create(open_task_err, 8000, NULL);
        lv_timer_set_repeat_count(timer2,1);
    }
}

void open_task_conv(lv_timer_t * timer){
    //连接wifi
    wifi_connect();                                 
    //配置时间
    time_init();
    //获取当地天气
    getCityandWeather();
    while(!get_and_set_time()){
    i++;
    delay(200);
    if(i>10);break; }
    timer=lv_timer_create(open_task_1, 1000, NULL);
    lv_timer_set_repeat_count(timer,1); 
}


/*********************end****************************/
void setup(){
    //初始化屏幕
    Lvgl_TFT_Init();
    Serial.begin(115200);
    EEPROM.begin(4096);
    //EEPROM.write(3, 5);
    EEPROM.commit();//不能去掉，否则无法正确读取数据
    //读取设置有关数据
    yuyan_flag=EEPROM.readByte(0);
    back_flag=EEPROM.readByte(1);
    lianan_flag=EEPROM.readByte(2);
    printf("yuyan_flag=%d\r\n back_flag=%d\r\n  lianan_flag=%d\r\n",yuyan_flag,back_flag,lianan_flag);//测试时候使用
    //创建开机提示背景
    open_back=lv_obj_create(lv_scr_act());//起始背景
    lv_obj_center(open_back);
    lv_obj_set_size(open_back,240,240);
    lv_obj_set_style_radius(open_back,240,NULL);
    lv_obj_set_style_bg_color(open_back,lv_color_hex(0xB8B9BD),NULL);
    //lv_obj_set_scrollbar_mode(open_back, LV_SCROLLBAR_MODE_OFF);
    //创建开机提示标签
    label_open=lv_label_create(open_back);
    lv_obj_center(label_open);
    lv_obj_set_style_text_font(label_open,&siyuanrouti_20,NULL);
    lv_obj_set_style_text_color(label_open,lv_color_hex(0xF0F0F0),NULL);
    if(yuyan_flag){
    lv_label_set_text(label_open,"WiFi Connecting...");
    }
    else
    lv_label_set_text(label_open,"网络连接中...");
    timer4=lv_timer_create(open_task_conv, 3000, NULL);
    lv_timer_set_repeat_count(timer4,1);
} 


 void loop(){
     lv_tick_inc(1);/* le the GUI do its work */  
    lv_task_handler();  
} 
